package com.dingding.service.rentsale;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.paravalidate.ValiResult;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.facde.rentsale.IRentSaleCarService;
import com.dingding.facde.rentsale.dto.ReqPlaceRentSaleOrder;
import com.dingding.facde.rentsale.dto.ResGetRentSaleOrderStatus;
import com.dingding.facde.rentsale.enums.CancelRentSaleOrderEnum;
import com.dingding.facde.rentsale.enums.IsExistOrderEnum;
import com.dingding.facde.rentsale.enums.RentSaleOrderEnum;
import com.dingding.service.rentsale.core.dao.IRentSaleCarOrderDao;

/**
 * @Description:以租代购汽车接口类
 * @author fengshuonan
 * @date 2016年7月4日 下午2:45:52
 * @version 叮叮搭车
 * @since
 */
@Component("rentSaleCarService")
public class RentSaleCarServiceImpl implements IRentSaleCarService{

	@Resource(name = "rentSaleCarOrderDao")
	private IRentSaleCarOrderDao rentSaleCarOrderDao;
	
	/**
	 * @Description: 以租代售下单
	 * @param reqPlaceRentSaleOrder 请求参数封装
	 * @author fengshuonan
	 * @return RentSaleOrderEnum 返回状态码
	 * @since 2.3.0
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public RentSaleOrderEnum placeRentSaleOrder(ReqPlaceRentSaleOrder reqPlaceRentSaleOrder) {
		
		//验证请求参数是否为空
		ValiResult result = ValidateUtil.validateBeanIsAvaliable(reqPlaceRentSaleOrder, ReqPlaceRentSaleOrder.class);
		if(!result.isAvailable()){
			return RentSaleOrderEnum.REQUEST_PARA_NULL;
		}
		
		//判断是否存在已下的单
		if( this.rentSaleCarOrderDao.isExistThisOrder(reqPlaceRentSaleOrder.getUserId()) ){
			return RentSaleOrderEnum.EXIST_PLACED_ORDER;
		}
		
		//执行下单业务
		boolean flag = this.rentSaleCarOrderDao.placeRentSaleCarOrder(reqPlaceRentSaleOrder.getUserId()
				, reqPlaceRentSaleOrder.getCarId(), reqPlaceRentSaleOrder.getCityId());
		if(!flag){
			return RentSaleOrderEnum.PLACE_ORDER_ERROR;
		}
		
		return RentSaleOrderEnum.PLACE_ORDER_SUCCESS;
	}

	
	/**
	 * @Description: 判断当前用户是否存在以租代售订单
	 * @param userId 用户id
	 * @author fengshuonan
	 * @return IsExistOrderEnum 状态枚举
	 * @since 2.3.0
	 */
	@Override
	@Transactional(readOnly = true)
	public IsExistOrderEnum isExistOrder(String userId) {
		
		//验证请求参数是否为空
		if(ToolUtil.isBlank(userId)){
			return IsExistOrderEnum.USER_ID_IS_NULL;
		}
		
		//判断该用户是否存在订单
		boolean flag = this.rentSaleCarOrderDao.isExistThisOrder(userId);
		if(flag == true){
			return IsExistOrderEnum.HAVE_PLACED_ORDER;
		}else{
			return IsExistOrderEnum.NO_PLACED_ORDER;
		}
	}


	/**
	 * @Description: 获取某 用户的以租代售订单的状态
	 * @param userId 用户id
	 * @author fengshuonan
	 * @return ResGetRentSaleOrderStatus 订单详情的封装
	 * @since 2.3.2
	 */
	@Override
	@Transactional(readOnly = true)
	public ResGetRentSaleOrderStatus getRentCarOrderStatus(String userId) {
		
		if(ToolUtil.isBlank(userId)){
			return null;
		}
		
		//获取该用户的所有以租代售订单
		List<ResGetRentSaleOrderStatus> list = this.rentSaleCarOrderDao.getRentSaleOrder(userId);
		
		if(list != null){
			for(ResGetRentSaleOrderStatus item : list){
				if(item.getOrderStatus() == 1){//如果订单里有已经预约的订单
					return item;
				}
			}
		}
		
		return null;
	}


	/**
	 * @Description: 取消订单
	 * @param userId 用户id
	 * @author fengshuonan
	 * @since 2.3.0
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public CancelRentSaleOrderEnum cancelRentCarOrder(String userId) {
		
		if(ToolUtil.isBlank(userId)){
			return CancelRentSaleOrderEnum.USERID_IS_NULL;
		}
		
		return rentSaleCarOrderDao.cancelRentSaleCarOrder(userId);
	}
	
}
